package com.cookie;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/ck1")
public class ck1 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //创建了一个cookie对象
        Cookie cookie1 = new Cookie("ck1","ck1value");
        Cookie cookie2 = new Cookie("ck2","ck2value");
        Cookie cookie3 = new Cookie("ck3","ck3value");
        Cookie cookie4 = new Cookie("ck4","ck4value");
        //通过resp把cookie放到客户端
        resp.addCookie(cookie1);
        resp.addCookie(cookie2);
        resp.addCookie(cookie3);
        resp.addCookie(cookie4);
        //设置有效期
        //不设置有效期 数据保存在浏览器缓存中，关闭浏览器就不行
        //设置有效期，保存在本地磁盘中，有效时间根据有效期设定 单位为秒
        cookie1.setMaxAge(60);
        cookie2.setMaxAge(60);
        //---------------获取cookie
        Cookie[] cks = req.getCookies();
        for (Cookie ck:cks) {
            String name = ck.getName();
            String value = ck.getValue();
            System.out.println(name+":"+value);
        }
    }
}
